package com.javabi.common.text.parser;

import java.util.concurrent.TimeUnit;

import com.javabi.common.date.UnitTime;

public class UnitTimeParser extends ObjectParser<UnitTime> {

	@Override
	protected UnitTime parseObject(String value) {
		int space = value.indexOf(' ');
		if (space == -1) {
			throw new IllegalArgumentException("Badly formatted UnitTime: '" + value + "'");
		}

		// Parse time
		String timePart = value.substring(0, space).trim();
		long time = Long.parseLong(timePart);

		// Parse unit
		String unitPart = value.substring(space + 1).trim();
		TimeUnit unit = TimeUnit.valueOf(unitPart);

		return new UnitTime(time, unit);
	}

}
